package com.kaili.basic.controlle;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.*;
import com.kaili.basic.pojo.entity.BaseReportLossEntity;
import com.kaili.basic.pojo.vo.BaseMoveStoreExportVO;
import com.kaili.basic.pojo.vo.BaseReportLossDetailVO;
import com.kaili.basic.service.BaseReportLossService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.exception.RestException;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import com.kaili.common.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@ApiVersion(1)
@RestController
@RequestMapping("/{api_version}")
@Transactional
@Api(tags = "报损管理", description = "报损管理")
public class BaseReportLossController {
    @Autowired
    private BaseReportLossService baseReportLossService;

    @GetMapping("/reportLoss/orderNo")
    @ApiOperation(value = "查询订单号")
    public Long queryStoreOrderNo(){
        return baseReportLossService.queryStoreOrderNo();
    }

    @GetMapping("/reportLoss")
    @ApiOperation(value = "查询报损单", notes = "查询报损单")
    public Page<BaseReportLossDetailVO> queryPageList(@ModelAttribute BaseReportLossQueryDto queryDto){
        return baseReportLossService.queryPageList(new Page<BaseReportLossDetailVO>(queryDto.getPage(),queryDto.getPageSize()),queryDto);
    }

    @GetMapping("/reportLoss/detail/{orderNo}")
    @ApiOperation(value = "查询报损明细")
    List<BaseReportLossDetailVO> queryDetailByOrderNo(@ApiParam("报损号") @PathVariable("orderNo") String orderNo){
        return baseReportLossService.queryDetailByOrderNo(new HashMap<String,Object>(){{put("orderNo",orderNo);}});
    }

    @GetMapping("/reportLoss/edit/{id}")
    @ApiOperation(value = "编辑查询报损")
    public BaseReportLossDetailVO queryReportLossDetail(@PathVariable("id") Long id){
        return baseReportLossService.queryReportLossDetail(new HashMap<String, Object>() {{
            put("id", id);
        }});
    }

    @GetMapping("/reportLoss/export")
    @ApiOperation(value = "入库明细导出")
    public void export(HttpServletResponse response, @ModelAttribute BaseReportLossQueryDto queryDto) throws RestException {
        List<BaseMoveStoreExportVO> list = baseReportLossService.export(queryDto);
        ExcelUtil.exportExcel(list,"报损管理","sheet1", BaseMoveStoreExportVO.class, UUID.randomUUID().toString()+".xls",response);
    }

    @PostMapping("/reportLoss")
    @ApiOperation(value = "新增报损")
    public RestStatus addReportLoss(@RequestBody BaseReportLossAddDTO dto){
        return baseReportLossService.addReportLoss(dto);
    }

    @PutMapping("/reportLoss/audit")
    @ApiOperation(value = "报损审核")
    public RestStatus auditReportLoss(@RequestBody BaseReportLossAuditDTO dto){
        try{
            return baseReportLossService.auditReportLoss(dto);
        }catch (Exception e){
            e.printStackTrace();
            return CommonStatus.STOCK_NOT_HAVE;
        }
    }

    @PutMapping("/reportLoss")
    @ApiOperation(value = "修改报损")
    public RestStatus updateReportLoss(@RequestBody BaseReportLossEditDTO dto){
        return baseReportLossService.updateReportLoss(dto);
    }

    @DeleteMapping("/reportLoss/{id}")
    @ApiOperation(value = "删除移库")
    public RestStatus deleteReportLoss(@ApiParam("订单号id")@PathVariable("id") long id){
        BaseReportLossEntity entity=new BaseReportLossEntity();
        entity.setId(id);
        entity.setStatus("0");
        baseReportLossService.updateById(entity);
        return CommonStatus.DELETE_OK;
    }
}


